package com.cqupt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.exception.BusinessException;
import com.cqupt.model.pojo.Sentence;
import com.cqupt.model.vo.ResultCodeEnum;
import com.cqupt.model.vo.SentenceVO;
import com.cqupt.service.SentenceService;
import com.cqupt.mapper.SentenceMapper;
import com.cqupt.service.SparkService;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class SentenceServiceImpl extends ServiceImpl<SentenceMapper, Sentence>
        implements SentenceService {

    @Resource
    private SentenceMapper sentenceMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SparkService sparkService;

    @Override
    public SentenceVO getSentence() {
        Integer userId = AuthContextUtil.get().getId();
        String json = stringRedisTemplate.opsForValue().get("userSentence:" + userId + ":");
        if (!StrUtil.isEmpty(json)) {
            Sentence sentence = JSON.parseObject(json, Sentence.class);
            SentenceVO sentenceVO = new SentenceVO();
            BeanUtil.copyProperties(sentence, sentenceVO);
            return sentenceVO;
        }
        LambdaQueryWrapper<Sentence> wrapper = new LambdaQueryWrapper<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = sdf.format(date);
        wrapper.eq(Sentence::getDate, format);
        Sentence sentence = sentenceMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(sentence)) {
            SentenceVO sentenceVO = new SentenceVO();
            BeanUtil.copyProperties(sentence, sentenceVO);
            return sentenceVO;
        }
        String str = "帮我生成一句英文的名言警句，并给出中文。不要生成之前已经生成过的。英文和中文之间用@隔开。";
        String res = sparkService.chat(9999, str);
        String[] parts = res.split("@");
        if (parts.length == 2) {
            String sentenceEn = parts[0].trim();
            String sentenceCh = parts[1].trim();
            Sentence sentence1 = new Sentence();
            sentence1.setSentenceEn(sentenceEn);
            sentence1.setSentenceCh(sentenceCh);
            sentence1.setDate(format);
            sentenceMapper.insert(sentence1);
            SentenceVO sentenceVO = new SentenceVO();
            BeanUtil.copyProperties(sentence1, sentenceVO);
            return sentenceVO;
        }
        return null;
    }

    @Override
    public List<Sentence> getChange() {

        List<Sentence> list = sentenceMapper.selectList(null);
        if (ObjectUtil.isEmpty(list)) {
            throw new BusinessException(ResultCodeEnum.DATA_ERROR);
        }
        if (list.size() > 10) {
            Collections.shuffle(list);
            return list.stream().limit(10).toList();
        } else return list;
    }

    @Override
    public void change(Integer id) {
        Integer userId = AuthContextUtil.get().getId();
        Sentence sentence = sentenceMapper.selectById(id);
        String json = JSON.toJSONString(sentence);
        stringRedisTemplate.opsForValue().set("userSentence:" + userId + ":", json, 1, TimeUnit.DAYS);
    }
}




